<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once(APPPATH."libraries/AdminController.php");
include_once(APPPATH."libraries/excel/reader.php");
class Upload_pagaris extends AdminController {  
	var $bulan = array();
	var $bav_type = "";
	var $bav_title = "";
	function __construct()   
	{
		parent::__construct();    
		$this->_set_action();
		$this->bav_type = 'garis';
		$this->bav_title = 'Garis';
		$this->_set_action(array("detail","delete"),"ITEM");
		$this->_set_title('Transaksi > Upload Excel PA '.$this->bav_title.' PT BAV ');
		$this->DATA->table="tm_bav";
		$this->cat_search = array(
			'all'					=> 'Pilih Kategory',
			'cabang_name'			=> 'Nama Cabang'
		);
		$this->folder_view = "transaksi/";
		$this->_set_cat_search(array("all"	=> $this->cat_search));	
		$this->load->model("mdl_upload_excel_bav","A");
		$this->load->model("mdl_cabang","MC");
		$this->upload_path="./uploaded/excel/";
		$this->prefix_view = "upload_bav";
		$this->bulan = array(
			"01"	=> "Januari",
			"02"	=> "Februari",
			"03"	=> "Maret",
			"04"	=> "April",
			"05"	=> "Mei",
			"06"	=> "Juni",
			"07"	=> "Juli",
			"08"	=> "Agustus",
			"09"	=> "September",
			"10"	=> "Oktober",
			"11"	=> "November",
			"12"	=> "Desember"
		);
		
		$this->output->enable_profiler(FALSE);
	}

//        http://aapialang.co.id/siaapa/transaksi/upload_paagunan/index
	function index(){
		
		$this->jCfg['isearch']['status'] = "all";
		$this->jCfg['isearch']['per_page'] = '50';
		$this->jCfg['isearch']['start_date'] = date("Y-m-d");
		$this->jCfg['isearch']['end_date'] = date("Y-m-d");
		$this->_releaseSession();
		
		$this->_set_desc_title('Daftar Upload Excel PA '.$this->bav_title.' PT BAV');	
				
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
				"limit" 	=> $this->per_page, 
				"start_date"=> $this->jCfg['isearch']['start_date'],
				"type"		=> $this->bav_type,
				"end_date"	=> $this->jCfg['isearch']['end_date'],
				"offset" 	=> $this->uri->segment($this->uri_segment)
		) );
		
		$data = $this->_data(array(
				'base_url'		=> $this->own_link.'index'
			)
		);	
                					
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
	
	function search(){
		$this->_set_desc_title('Daftar Upload Excel PA '.$this->bav_title.' PT BAV');	
		$this->load->helper('url');		
		$this->uri_segment = 6;
			
		$column = ''; $keyword = '';				
		if( isset($_POST['column']) && isset($_POST['keyword']) ){
			$column = $_POST['column'];
			$keyword = url_title($_POST['keyword']);
			$this->setiSearch(array(
				"start_date"=> dbClean($_POST['dstart']),
				"end_date"	=> dbClean($_POST['dend']),
				"per_page"	=> dbClean($_POST['per_page'])
			));
		}else{
			$column =  $this->uri->segment(4);
			$keyword =   $this->uri->segment(5);
		}	
				
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
								"limit" 	=> $this->per_page, 
								"offset" 	=> $this->uri->segment($this->uri_segment),
								"column"	=> $column,
								"type"		=> $this->bav_type,
								"start_date"=> $this->jCfg['isearch']['start_date'],
								"end_date"	=> $this->jCfg['isearch']['end_date'],
								"keyword"	=> (trim($keyword)=="all")?"":$keyword
							) );
		$k=($keyword=="")?"all":$keyword;
		$data = $this->_data(
			array(
				'base_url'		=>$this->own_link.'search/'.$column.'/'.$k
			)
		);	
		
		
		$this->header_page['key']=pTxt(($k=="all")?"":$k);
		$this->header_page['col']=$column;	
		$this->header_page['cat_search'] = array($column	=>	$this->cat_search);	
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
// Disini sayya agak bingung tolong bantu dunk	
// http://aapialang.co.id/siaapa/transaksi/upload_pagaris/add        
	function add(){		
		$this->_set_desc_title('Upload Excel PA '.$this->bav_title.' PT. BAV');
		$data['cabang'] = $this->MC->data();	                
		
		$m = $this->DATA->data_id(array(
			"bav_status"	=> 0,
			"bav_type"		=> $this->bav_type
		));	
		
		if(count($m) > 0){
			$this->DATA->table = "tm_bav_detail";
//$qry disini tidak bisa simpan data ke table tm_bav_detail
			$qry = $this->db->query("SELECT * FROM ".$this->DATA->table." WHERE detail_bav = '".$m->bav_id."' 
					ORDER BY detail_order ASC")->result();
			$data['items'] = $qry;
			$data['data'] = $m;
		}
			
		$this->_v($this->folder_view.$this->prefix_view."_add",$data);
	}
//data diatas kok engk masuk apa yang salah yach
        
        
	function detail($id=""){
		if(trim($id)=="")
			redirect($this->own_link);	
			
		$this->_set_desc_title('Detail Upload Excel PA '.$this->bav_title.' PT. BAV');

		$m = $this->DATA->data_id(array(
			"bav_id"		=> dbClean($id),
			"bav_status"	=> 1
		));	
		
		if(count($m) > 0){
			$this->DATA->table = "tm_bav_detail";
			$qry = $this->db->query("SELECT * FROM ".$this->DATA->table." WHERE detail_bav = '".$m->bav_id."' 
					ORDER BY detail_order ASC")->result();
			$data['items'] = $qry;
			$data['data'] = $m;
                        
		}else{
			redirect($this->own_link);		
		}	
		$this->_v($this->folder_view.$this->prefix_view."_detail",$data);
	}
	
	function delete($id=""){
		if(trim($id)=="")
			redirect($this->own_link);	

		$m = $this->DATA->data_id(array(
			"bav_id"		=> dbClean($id)
		));	
		
		if(count($m) > 0){		
			$this->DATA->_delete(array(
				"bav_id"		=> $m->bav_id
			));	
			$this->DATA->table = "tm_bav_detail";
			$this->DATA->_delete(array(
				"detail_bav"		=> $m->bav_id
			));
		}
		redirect($this->own_link);		
		
	}
	
	function hapus_data(){
		$this->del_data();
		redirect($this->own_link."add");
	}
	function approve_data(){
		$m = $this->DATA->data_id(array(
			"bav_status"	=> 0
		));	
		if(count($m) > 0){
			$this->DATA->_update(array(
				"bav_id"	=> $m->bav_id
			),array(
				"bav_status"	=> 1
			));	
		}
		redirect($this->own_link."add");
	}
	function del_data(){
		$m = $this->DATA->data_id(array(
			"bav_status"	=> 0
		));	
		if(count($m) > 0){
			$this->DATA->table = "tm_bav_detail";
			$this->DATA->_delete(array(
				"detail_bav"	=> $m->bav_id
			));
			
			$this->DATA->table = "tm_bav";
			$this->DATA->_delete(array(
				"bav_id"	=> $m->bav_id
			));
		}
	}
	
	function reader($p=array()){		
		
		$data = new Spreadsheet_Excel_Reader();
		$data->setOutputEncoding('CP1251');
		$data->read($this->upload_path.$p['file']);
		error_reporting(E_ALL ^ E_NOTICE);
		
		$this->DATA->table = "tm_bav_detail";
		$startRead = getCfgApp('read_row_'.$this->bav_type);
		for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
			if( $i>=$startRead && !empty($data->sheets[0]['cells'][$i][1]) ){
				// inser data per record... 
				$dataInput = array(
					'detail_bav'		=> $p['id'],
					'detail_order'		=> $data->sheets[0]['cells'][$i][1],
					'detail_nama'		=> $data->sheets[0]['cells'][$i][2],
					'detail_alamat'		=> $data->sheets[0]['cells'][$i][3],
					'detail_tgllahir'	=> $data->sheets[0]['cells'][$i][5],
					'detail_tglakad'	=> $data->sheets[0]['cells'][$i][4],
					'detail_usia'		=> $data->sheets[0]['cells'][$i][6],
					'detail_periode_ptg'=> $data->sheets[0]['cells'][$i][7],
					'detail_uang_ptg'	=> $data->sheets[0]['cells'][$i][8],
					'detail_tarif_premi'=> $data->sheets[0]['cells'][$i][9],
					'time_add'			=> date("Y-m-d H:i:s"),
					'user_add'			=> $this->jCfg['user']['id'],
					'app_id'			=> $this->jCfg['app_id']
				);
				
				if($this->bav_type == 'agunan'){
					$dataInput['detail_obj_ptg'] = $data->sheets[0]['cells'][$i][10];
				}
				
				$this->DATA->_add($dataInput);			
			}
			
		}
	}
	
	function save(){
		// delete existing data ..
		$this->del_data();
		// simpan data tm_bav
		$data = array(
			'bav_cabang' 	=> dbClean($_POST['cabang']),
			'bav_type' 		=> $this->bav_type,
			'bav_status'	=> 0,
			'bav_bulan' 	=> dbClean($_POST['tahun'])."-".dbClean($_POST['bulan'])
		);
                

		$a = $this->_save_master( 
			$data,
			array(
				'bav_id' => dbClean($_POST['bav_id'])
			),
			dbClean($_POST['bav_id'])			
		);
		
		foreach($_FILES as $k => $v){
			$this->upload_formulir(
				array(
					'id'		=> $a['id'],
					'input'		=> $k
				)
			);
		}
		
		redirect($this->own_link."add");
	}
	
	function upload_formulir($par=array()){
		if($_FILES[$par['input']]['error']==4)
			return false;
		
		$uId = rand(1,100000)."-".uniqid();
		$uri = $this->upload_path;
						
		$fileName = $uId;
		$config['upload_path'] 		= $uri."/";
		$config['file_name'] 		= $fileName;
		$config['allowed_types'] 	= 'xls|xlsx';
		$config['max_size']			= 1024*10;
		$this->load->library('upload');
		
		$this->upload->initialize($config);
	
		if( $this->upload->do_upload($par['input']) )
		{	
			$file = $uId.$this->upload->file_ext;		
			$this->reader(array(
				"file"	=> $file,
				"id"	=> $par['id']
			));
		}
		else{
			
			echo $this->upload->display_errors('<div style="clear:both;">', '</div>'); //OKIE WARDOYO
			break;
		}
	}

}
